package com.chenfeng.ego.portal.controller;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.chenfeng.ego.commons.pojo.SearchResult;
import com.chenfeng.ego.portal.service.TbItemService;

@Controller
public class TbItemController
{

	@Resource
	private TbItemService itemService;
	@RequestMapping("search.html")
	public String search(String q,@RequestParam(defaultValue="1")int page,@RequestParam(defaultValue="10") int rows,Model model )
	{
		try
		{
			q = new String(q.getBytes("iso-8859-1"),"utf-8");
			System.out.println(q);
		} catch (UnsupportedEncodingException e)
		{
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			return "index";
		}
		SearchResult searchResult = itemService.search(q, page, rows);
		model.addAttribute("itemList",searchResult.getItemList());
		model.addAttribute("page", searchResult.getPage());
		model.addAttribute("totalPages", searchResult.getTotalPages());
		model.addAttribute("query", q);
		return "search";
	}
}
